package com.polaris.live.adapter

import android.view.View
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.databinding.ItemMePhotoBinding
import com.polaris.live.resp.back_resp.UserOtherPhotoVo
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.normal

/**
 * 个人相册
 *
 * @author Created by 长空 on 2023/10/28/022 11:10
 */
class MePhotoAdapter : BaseBindingAdapter<ItemMePhotoBinding, UserOtherPhotoVo>() {

    private var selectPosition = -1

    init {
        addChildClickViewIds(R.id.iv_delete)
    }

    override fun convert(holder: VBViewHolder<ItemMePhotoBinding>, item: UserOtherPhotoVo) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition
            if (position == itemCount - 1 && item.url.isEmpty()) {
                viewBg.alpha = 1f
                ivPhotoCover.visibility = View.VISIBLE
                ivPhoto.visibility = View.GONE
                ivEye.visibility = View.GONE
            } else {
                if (item.status == 1) {
                    ivEye.visibility = View.GONE
                } else {
                    ivEye.visibility = View.VISIBLE
                }
                if (position == selectPosition) {
                    ivPhoto.alpha = 1f
                    viewBg.alpha = 1f
                    ivEye.alpha = 1f
                } else {
                    ivPhoto.alpha = 0.3f
                    viewBg.alpha = 0.3f
                    ivEye.alpha = 0.3f
                }
                ivPhoto.visibility = View.VISIBLE
                ivPhotoCover.visibility = View.GONE
                Glide.with(context).load(ImageUtils.imgPathSmall(item.url)).normal().into(ivPhoto)
            }
            if (item.status == 1 && position == selectPosition) {
                ivDelete.visibility = View.VISIBLE
                if (item.photoId == CommonConst.CoverPhoto.COVERPHOTOID) { //是封面照
                    ivDelete.setImageResource(R.drawable.btn_delete)
                } else {
                    ivDelete.setImageResource(R.drawable.btn_me_photo_delete)
                }

            } else {
                ivDelete.visibility = View.INVISIBLE
            }
        }
    }

    fun setSelect(position: Int) {
        selectPosition = position
        notifyDataSetChanged()
    }

    fun clearSelected() {
        if (selectPosition == -1) {
            return
        }
        val index = selectPosition
        selectPosition = -1
        notifyItemChanged(index)
    }

}